# coding: utf-8
from tortoise import fields
from core.base_db import BaseModel


class ChatApplication(BaseModel):
    APPLICATION_TYPE_PUBLIC = 1
    APPLICATION_TYPE_PRIVATE = 2

    title = fields.CharField(max_length=255, description="标题")
    desc = fields.TextField(description="描述")
    icon = fields.TextField(description="图标")
    instruct = fields.TextField(description="指令")
    examples = fields.TextField(description="示例模板")
    user_id = fields.ForeignKeyField(model_name="models.ZhanUser", null=True, default=None)
    application_type = fields.IntField(default=APPLICATION_TYPE_PUBLIC, description="应用类型：公共1，个人2")

    class Meta:
        table = "chat_application"


class ChatMessage(BaseModel):
    MESSAGE_ACTION_USER = 1
    MESSAGE_ACTION_AI = 2

    content = fields.TextField(description="信息")
    message_action = fields.IntField(default=MESSAGE_ACTION_USER, description="消息类型：用户1，AI2")
    owner_chat = fields.ForeignKeyField(model_name="models.ChatSession")
    owner_user = fields.ForeignKeyField(model_name="models.ZhanUser", null=True, default=None)
    role = fields.CharField(max_length=255, description="发送者")

    class Meta:
        table = "chat_message"


class ChatSession(BaseModel):
    session_title = fields.CharField(max_length=255, description="会话标题")
    model = fields.CharField(max_length=255, description="会话模型", default="")
    object = fields.CharField(max_length=255, default="")
    user_id = fields.ForeignKeyField(model_name="models.ZhanUser", null=True, default=None)
    owner_key = fields.ForeignKeyField(model_name="models.ChatExclusiveKey", null=True, default=None)

    class Meta:
        table = "chat_session"


class ChatExclusiveKey(BaseModel):
    exclusive_key = fields.TextField(description="密钥")
    count = fields.IntField(description="次数", default=200)
    use_count = fields.IntField(description="已用次数", default=0)
    expiration_time = fields.DatetimeField(description="过期时间")
    order_id = fields.IntField(null=True, description="订单ID")
    user_id = fields.ForeignKeyField(model_name="models.ZhanUser", null=True, default=None)

    class Meta:
        table = "chat_exclusive_key"
